package com.hengxinguotong.hxgtwygsp.ui.supervise.patrol.statistics;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hengxinguotong.hxgtwygsp.R;
import com.hengxinguotong.hxgtwygsp.model.Year;
import com.hengxinguotong.hxgtwygsp.ui.base.BaseFragment;
import com.hengxinguotong.hxgtwygsp.widget.DropListPW;
import com.hengxinguotong.hxgtwygsp.widget.RingView;
import com.hengxinguotong.hxgtwygsp.widget.YearPW;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

public class PatrolStatisticsFragment extends BaseFragment implements PatrolStatisticsContract.IView
        , DropListPW.OnClickCallback<Year> {
    @Inject
    PatrolStatisticsContract.IPresenter mPresenter;

    @BindView(R.id.ring_view)
    RingView ringView;
    @BindView(R.id.patrol_rectified_num)
    TextView patrolRectifiedNum;
    @BindView(R.id.patrol_rectifying_num)
    TextView patrolRectifyingNum;
    @BindView(R.id.patrol_rectify_num)
    TextView patrolRectifyNum;
    @BindView(R.id.patrol_rectified_rate)
    TextView patrolRectifiedRate;
    @BindView(R.id.patrol_rectifying_rate)
    TextView patrolRectifyingRate;
    @BindView(R.id.patrol_rectify_rate)
    TextView patrolRectifyRate;
    @BindView(R.id.patrol_year)
    TextView mPatrolYear;
    @BindView(R.id.patrol_year_container)
    RelativeLayout mPatrolYearContainer;
    @BindView(R.id.patrol_rectified)
    TextView mPatrolRectified;
    @BindView(R.id.patrol_rectifying)
    TextView mPatrolRectifying;
    @BindView(R.id.patrol_rectify_will)
    TextView mPatrolRectifyWill;

    private List<Year> yearList;

    public PatrolStatisticsFragment() {
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mPresenter.bindView(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.dropView();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_patrol_statistics;
    }

    @Override
    public void init() {
        mockView();
        yearList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        mPatrolYear.setText(getString(R.string.patrol_year, calendar.get(Calendar.YEAR)));
        Year year;
        for (int i = 0; i < 3; i++)
        {
            year = new Year();
            year.setName(getString(R.string.patrol_year, calendar.get(Calendar.YEAR)));
            yearList.add(year);
            calendar.add(Calendar.YEAR, -1);
        }
    }

    private void mockView() {
        Resources resources = getResources();
        int[] colors = {resources.getColor(R.color.c019FFF),resources.getColor(R.color.cFE5660), resources.getColor(R.color.c7250FC)};
        ringView.setColors(colors);
        float[] values = {92f, 5f ,3f};
        ringView.setValues(values);

        patrolRectifiedNum.setText(String.format(getString(R.string.patrol_rectify_num),36));
        patrolRectifiedRate.setText("92%");
        patrolRectifyingNum.setText(String.format(getString(R.string.patrol_rectify_num),2));
        patrolRectifyingRate.setText("5%");
        patrolRectifyNum.setText(String.format(getString(R.string.patrol_rectify_num),1));
        patrolRectifyRate.setText("3%");

    }

    @OnClick(R.id.patrol_year_container)
    public void onViewClicked(View view) {
        YearPW yearPW = new YearPW(getContext(), view.getWidth());
        yearPW.setOnClickCallback(this);
        yearPW.initData(yearList, 0);
        yearPW.showAsDropDown(view);
    }


    @Override
    public void onClickPW(Year year) {
        mPatrolYear.setText(year.getName());
    }



}
